package com.atguigu.srb.common.exception;

import com.atguigu.srb.common.result.R;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Component
@RestControllerAdvice
public class UnifiedExceptionHandler {
    // 通用异常
    @ExceptionHandler(Exception.class)
    public R exceptionHandler(Exception e){
        System.out.println("通用异常处理通知: "+e.getMessage());
        return R.ERROR().message("系统出现异常，请联系管理员");
    }

    // SQL异常
    @ExceptionHandler(BadSqlGrammarException.class)
    public R sqlExceptionHandler(BadSqlGrammarException e){
        System.out.println("SQL语句异常处理通知: "+e.getMessage());
        return R.ERROR().message("SQL语法错误");
    }

    // 自定义异常
    @ExceptionHandler(BusinessException.class)
    public R businessExceptionHandler(BusinessException e){
        System.out.println("业务异常处理通知: "+e.getMessage());
        return R.ERROR().message(e.getMessage());
    }
}
